// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import zombie.characters.IsoPlayer;
import zombie.core.Color;
import zombie.core.Core;
import zombie.core.textures.Texture;
import zombie.iso.IsoCamera;
import zombie.iso.IsoUtils;

public final class ActionProgressBar extends UIElement {
    Texture background;
    Texture foreground;
    float deltaValue = 1.0F;
    float animationProgress = 0.0F;
    public int delayHide = 0;
    private final int offsetX;
    private final int offsetY;

    public ActionProgressBar(int x, int y) {
        this.background = Texture.getSharedTexture("BuildBar_Bkg");
        this.foreground = Texture.getSharedTexture("BuildBar_Bar");
        this.offsetX = x;
        this.offsetY = y;
        this.width = (float)this.background.getWidth();
        this.height = (float)this.background.getHeight();
        this.followGameWorld = true;
    }

    @Override
    public void render() {
        if (this.isVisible() && UIManager.VisibleAllUI) {
            this.DrawUVSliceTexture(
                this.background, 0.0, 0.0, (double)this.background.getWidth(), (double)this.background.getHeight(), Color.white, 0.0, 0.0, 1.0, 1.0
            );
            if (this.deltaValue == Float.POSITIVE_INFINITY) {
                if (this.animationProgress < 0.5F) {
                    this.DrawUVSliceTexture(
                        this.foreground,
                        3.0,
                        0.0,
                        (double)this.foreground.getWidth(),
                        (double)this.foreground.getHeight(),
                        Color.white,
                        0.0,
                        0.0,
                        (double)(this.animationProgress * 2.0F),
                        1.0
                    );
                } else {
                    this.DrawUVSliceTexture(
                        this.foreground,
                        3.0,
                        0.0,
                        (double)this.foreground.getWidth(),
                        (double)this.foreground.getHeight(),
                        Color.white,
                        (double)((this.animationProgress - 0.5F) * 2.0F),
                        0.0,
                        1.0,
                        1.0
                    );
                }
            } else {
                this.DrawUVSliceTexture(
                    this.foreground,
                    3.0,
                    0.0,
                    (double)this.foreground.getWidth(),
                    (double)this.foreground.getHeight(),
                    Color.white,
                    0.0,
                    0.0,
                    (double)this.deltaValue,
                    1.0
                );
            }
        }
    }

    public void setValue(float delta) {
        this.deltaValue = delta;
    }

    public float getValue() {
        return this.deltaValue;
    }

    public void update(int nPlayer) {
        if (this.deltaValue == Float.POSITIVE_INFINITY) {
            this.animationProgress = this.animationProgress + 0.02F * zombie.GameTime.getInstance().getRealworldSecondsSinceLastUpdate() * 60.0F;
            if (this.animationProgress > 1.0F) {
                this.animationProgress = 0.0F;
            }

            this.setVisible(true);
            this.updateScreenPos(nPlayer);
            this.delayHide = 2;
        } else {
            if (this.getValue() > 0.0F && this.getValue() < 1.0F) {
                this.setVisible(true);
                this.delayHide = 2;
            } else if (this.isVisible() && this.delayHide > 0 && --this.delayHide == 0) {
                this.setVisible(false);
            }

            if (!UIManager.VisibleAllUI) {
                this.setVisible(false);
            }

            if (this.isVisible()) {
                this.updateScreenPos(nPlayer);
            }
        }
    }

    private void updateScreenPos(int _int) {
        IsoPlayer isoPlayer = IsoPlayer.players[_int];
        if (isoPlayer != null) {
            float float0 = IsoUtils.XToScreen(isoPlayer.getX(), isoPlayer.getY(), isoPlayer.getZ(), 0);
            float float1 = IsoUtils.YToScreen(isoPlayer.getX(), isoPlayer.getY(), isoPlayer.getZ(), 0);
            float0 = float0 - IsoCamera.getOffX() - isoPlayer.offsetX;
            float1 = float1 - IsoCamera.getOffY() - isoPlayer.offsetY;
            float1 -= (float)(128 / (2 / Core.TileScale));
            float0 /= Core.getInstance().getZoom(_int);
            float1 /= Core.getInstance().getZoom(_int);
            float0 -= this.width / 2.0F;
            float1 -= this.height;
            if (isoPlayer.getUserNameHeight() > 0) {
                float1 -= (float)(isoPlayer.getUserNameHeight() + 2);
            }

            this.setX((double)(float0 + (float)this.offsetX));
            this.setY((double)(float1 + (float)this.offsetY));
        }
    }
}
